package com.blast.comicsisland.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

/**
 * Created by ibm on 2015/6/10.
 */
public class RequestManager {
    private static RequestManager ourInstance;

    public static RequestManager createInstance (Context context){
        if (context != null){
            if (ourInstance == null){
                ourInstance = new RequestManager(context);
            }
        }else {
            throw new IllegalArgumentException("Context must be set");
        }
        return ourInstance;
    }

    public static RequestManager getInstance() {
        return ourInstance;
    }
    //单例当中除了构造和成员变量，其他的最好都是public (static)
    private RequestQueue requestQueue;
    private ImageLoader imageLoader;
    private RequestManager(Context context) {
        requestQueue = Volley.newRequestQueue(context);
        imageLoader = new ImageLoader(
                requestQueue,
                new ImageLoader.ImageCache() {
                    private LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(20);
                    @Override
                    public Bitmap getBitmap(String url) {
                        return cache.get(url);
                    }

                    @Override
                    public void putBitmap(String url, Bitmap bitmap) {
                        cache.put(url, bitmap);
                    }
                }
        );
    }
    public RequestQueue getRequestQueue(){
        return requestQueue;
    }

    public ImageLoader getImageLoader(){
        return imageLoader;
    }
}
